Skip to content

fix(segment-button): update checked state on render#26970

Merged
liamdebeasi merged 13 commits into
mainfrom
fix/v7-segment-button-checked
Apr 12, 2023
Merged

fix(segment-button): update checked state on render#26970
liamdebeasi merged 13 commits into
mainfrom
fix/v7-segment-button-checked

Conversation

@sean-perkins

@sean-perkins sean-perkins commented Mar 15, 2023

Copy link
Copy Markdown
Contributor

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
    • Some docs updates need to be made in the ionic-docs repo, in a separate PR. See the contributing guide for details.
  • Build (npm run build) was run locally and any changes were pushed
  • Lint (npm run lint) has passed locally and any fixes were made for failures

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

In v7, asynchronously assigning a value to ion-segment and ion-segment-button can cause the ion-segment to render without an active value (segment button does not show as checked).

Issue URL: resolves #26830

What is the new behavior?

  • The ion-segment-button checked state is synced with the parent ion-segment, when the component finishes the initial render.

Does this introduce a breaking change?

  • Yes
  • No

Other information

Dev-build: 7.0.0-dev.11678736268.18a0beb1

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@github-actions github-actions Bot added the package: core @ionic/core package label Mar 15, 2023
@lincolnthree

Copy link
Copy Markdown

Does this need to be tagged for review? What can I do to help this along?

@sean-perkins

Copy link
Copy Markdown
Contributor Author

Opening this up for review. I identified that calling updateState() resolves the problematic behavior in the reproduction (as well as Lincoln has done the same).

I left this in draft for a bit because I wanted to see if there is an easy way to write tests around this (the reproduction requires a bit of rxjs + Angular). I haven't had the time to do so yet.

I think the risk of this change is incredibly low (at worst, it is calling updateState() when the state is already set correctly). I'd feel comfortable creating a tech debt ticket to track adding test coverage to this in a follow-up sprint, in order to get this into the next RC.

@sean-perkins sean-perkins marked this pull request as ready for review March 28, 2023 18:46
@sean-perkins sean-perkins requested a review from a team as a code owner March 28, 2023 18:46
};
}

componentDidLoad() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Threading for discussion:

Are we confident this is a v7 regression? When I triaged this in #26830 (comment), I was able to reproduce this in v6, but perhaps I reproduced a different issue.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively: Is this a bug that's always been there but is now causing issues because of v7 changes? (possibly the initializeNextTick change?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I did a bit more digging and this is (fortunately) not an Ionic v7 regression. However, it is much more noticeable now that the initializeNextTick config was removed in Ionic v7.

The problem here is that if the value property on ion-segment-button is set asynchronously, the updateState method is not called again.

So given the following:

<ion-segment value="general">
  <ion-segment-button>General</ion-segment-button>
</ion-segment>

If you were to asynchronously set the value of the segment button to "general", the segment button would not appear checked.

Below is a test you can use to verify the fix.

segment/test/async/segment.e2e.ts
import { expect } from '@playwright/test';
import { test } from '@utils/test/playwright';

test.describe('segment: async', () => {
  test('should set checked state when value is set asynchronously', async ({ page, skip }) => {
    skip.rtl();
    skip.mode('md');

    await page.setContent(`
      <ion-segment value="first">
        <ion-segment-button>First</ion-segment-button>
      </ion-segment>
    `);

    const segmentButton = page.locator('ion-segment-button');

    await segmentButton.evaluate((el: HTMLIonSegmentButtonElement) => el.value = 'first');
    await page.waitForChanges();

    await expect(segmentButton).toHaveClass(/segment-button-checked/);
  });
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, we should merge this into main so v6 devs can benefit from this fix too.

Comment thread core/src/components/segment-button/segment-button.tsx Outdated
Comment thread core/src/components/segment-button/segment-button.tsx Outdated
@lincolnthree

Copy link
Copy Markdown

Thanks so much guys! Really happy you were able to figure out the root cause of what's happening.

Base automatically changed from feature-7.0 to main March 30, 2023 16:16
@lincolnthree

Copy link
Copy Markdown

Just looping back on this. Anything I can do to help? I noticed it didn't make it into v7, and the issue is occurring quite consistently for my application now. Sorry to be a pest! But I've seen a few releases out and this seems so close to the finish line.

@liamdebeasi

Copy link
Copy Markdown
Contributor

We pulled this into the next sprint to take another look at.

@liamdebeasi

Copy link
Copy Markdown
Contributor

@sean-perkins I added the correct fix as well as a test. Mind taking a look when you get a chance? (I changed from an E2E to a unit test too)

@sean-perkins

Copy link
Copy Markdown
Contributor Author

Looks good to me, I resolved the merge conflict with main. I can't review my own PRs, but I'll mark this as merge when ready.

We can cherry pick the commit into the v6 release branch.

@sean-perkins sean-perkins added this pull request to the merge queue Apr 12, 2023
@liamdebeasi liamdebeasi removed this pull request from the merge queue due to a manual request Apr 12, 2023
@liamdebeasi liamdebeasi added this pull request to the merge queue Apr 12, 2023
Merged via the queue into main with commit 16aa977 Apr 12, 2023
@liamdebeasi liamdebeasi deleted the fix/v7-segment-button-checked branch April 12, 2023 13:41
liamdebeasi added a commit that referenced this pull request Apr 12, 2023
<!-- Please refer to our contributing documentation for any questions on
submitting a pull request, or let us know here if you need any help:
https://ionicframework.com/docs/building/contributing -->

Please check if your PR fulfills the following requirements:
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been reviewed and added / updated if needed (for bug
fixes / features)
- Some docs updates need to be made in the `ionic-docs` repo, in a
separate PR. See the [contributing
guide](https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#modifying-documentation)
for details.
- [x] Build (`npm run build`) was run locally and any changes were
pushed
- [x] Lint (`npm run lint`) has passed locally and any fixes were made
for failures

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type, submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:
- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

<!-- Please describe the current behavior that you are modifying. -->

In v7, asynchronously assigning a value to `ion-segment` and
`ion-segment-button` can cause the `ion-segment` to render without an
active value (segment button does not show as checked).

<!-- Issues are required for both bug fixes and features. -->
Issue URL: resolves #26830

<!-- Please describe the behavior or changes that are being added by
this PR. -->

- The `ion-segment-button` checked state is synced with the parent
`ion-segment`, when the component finishes the initial render.

- [ ] Yes
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Dev-build: `7.0.0-dev.11678736268.18a0beb1` ✅

---------

Co-authored-by: Liam DeBeasi <liamdebeasi@users.noreply.github.com>
liamdebeasi added a commit that referenced this pull request Apr 12, 2023
This backports #26970
to v6.

resolves #26830

Co-authored-by: Sean Perkins <sean@ionic.io>
liamdebeasi added a commit that referenced this pull request Apr 17, 2023
<!-- Please refer to our contributing documentation for any questions on
submitting a pull request, or let us know here if you need any help:
https://ionicframework.com/docs/building/contributing -->

## Pull request checklist

Please check if your PR fulfills the following requirements:
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been reviewed and added / updated if needed (for bug
fixes / features)
- Some docs updates need to be made in the `ionic-docs` repo, in a
separate PR. See the [contributing
guide](https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#modifying-documentation)
for details.
- [x] Build (`npm run build`) was run locally and any changes were
pushed
- [x] Lint (`npm run lint`) has passed locally and any fixes were made
for failures


## Pull request type

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type, submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:
- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe): 


## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

In v7, asynchronously assigning a value to `ion-segment` and
`ion-segment-button` can cause the `ion-segment` to render without an
active value (segment button does not show as checked).

<!-- Issues are required for both bug fixes and features. -->
Issue URL: resolves #26830


## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- The `ion-segment-button` checked state is synced with the parent
`ion-segment`, when the component finishes the initial render.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Dev-build: `7.0.0-dev.11678736268.18a0beb1` ✅

---------

Co-authored-by: Liam DeBeasi <liamdebeasi@users.noreply.github.com>
liamdebeasi added a commit that referenced this pull request Apr 19, 2023
<!-- Please refer to our contributing documentation for any questions on
submitting a pull request, or let us know here if you need any help:
https://ionicframework.com/docs/building/contributing -->

## Pull request checklist

Please check if your PR fulfills the following requirements:
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been reviewed and added / updated if needed (for bug
fixes / features)
- Some docs updates need to be made in the `ionic-docs` repo, in a
separate PR. See the [contributing
guide](https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#modifying-documentation)
for details.
- [x] Build (`npm run build`) was run locally and any changes were
pushed
- [x] Lint (`npm run lint`) has passed locally and any fixes were made
for failures


## Pull request type

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type, submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:
- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe): 


## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

In v7, asynchronously assigning a value to `ion-segment` and
`ion-segment-button` can cause the `ion-segment` to render without an
active value (segment button does not show as checked).

<!-- Issues are required for both bug fixes and features. -->
Issue URL: resolves #26830


## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- The `ion-segment-button` checked state is synced with the parent
`ion-segment`, when the component finishes the initial render.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Dev-build: `7.0.0-dev.11678736268.18a0beb1` ✅

---------

Co-authored-by: Liam DeBeasi <liamdebeasi@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: core @ionic/core package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ion-segment does not reflect the initial value of [value] when rendering component

3 participants